<template>
  <el-dialog width="60%" top="10vh" :title="!dataForm.id ? '新增' : '修改'" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="请输入用户名" readonly></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="请输入手机号" readonly></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="headPic">
        <el-image style="width: 100px; height: 100px"
          :src=" baseApi + dataForm.headPic"
          :preview-src-list="[baseApi + dataForm.headPic]"
          fit="contain"
          lazy
          class="el-avatar"
        >
          <div slot="error">
            <i class="el-icon-document" />
          </div>
        </el-image>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="dataForm.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
          <el-radio :label="0">未知</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="简介" prop="introduction">
        <el-input v-model="dataForm.introduction" placeholder="请输入简介" readonly></el-input>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-input v-model="dataForm.birthday" placeholder="请输入生日" readonly></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <!-- <el-button type="primary" size="small" @click="dataFormSubmit()">确定</el-button> -->
      <el-button size="small" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getUserInfo, createUser, updateUser } from '@/api/knowledge/user';
const baseUrl = process.env.VUE_APP_FILE_URL === '/' ? '' : process.env.VUE_APP_FILE_URL;

const defaultDataForm = {
  id: '',
  username: '',
  mobile: '',
  password: '',
  headPic: '',
  gender: '',
  introduction: '',
  birthday: '',
}

export default {
  data() {
    return {
      baseApi: baseUrl,
      dataForm: Object.assign({}, defaultDataForm),
      visible: false,
      dataRule: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        headPic: [
          { required: true, message: '头像不能为空', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '性别 0 未知 1男 2 女不能为空', trigger: 'blur' }
        ],
        introduction: [
          { required: true, message: '简介不能为空', trigger: 'blur' }
        ],
        birthday: [
          { required: true, message: '生日不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getUserInfo(this.dataForm.id).then(res => {
            if (res && res.code === 200) {
              this.dataForm.username = res.data.username;
              this.dataForm.mobile = res.data.mobile;
              this.dataForm.password = res.data.password;
              this.dataForm.headPic = res.data.headPic;
              this.dataForm.gender = res.data.gender;
              this.dataForm.introduction = res.data.introduction;
              this.dataForm.birthday = res.data.birthday;
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let params = {
            id: this.dataForm.id,
            username: this.dataForm.username,
            mobile: this.dataForm.mobile,
            password: this.dataForm.password,
            headPic: this.dataForm.headPic,
            gender: this.dataForm.gender,
            introduction: this.dataForm.introduction,
            birthday: this.dataForm.birthday,
          }
          if (!this.dataForm.id) {
            // 创建
            createUser(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.dataForm = Object.assign({}, defaultDataForm);
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')
            })
          } else {
            // 更新
            updateUser(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.$message({
                message: '修改成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')
            })
          }
        }
      })
    },
  }
}
</script>
